package com.hui.dao;

import com.hui.dao.AdminDao;
import com.hui.entity.User;
import com.hui.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/06/19/16:10
 * @Description:
 */
public class AdminDaoImpl implements AdminDao {
    // 查询管理员是否存在
    @Override
    public boolean adminExist(User user) {
        Connection con = DruidUtil.getConnection();
        try {
            String sql = "select count(*) as num from user where username= ? and password= ?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ResultSet resultSet = ps.executeQuery();
            // 获取查询结果
            resultSet.next();
            int num = resultSet.getInt("num");
            // 关闭资源
            DruidUtil.close(con, ps, resultSet);
            return num == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
